package com.example.sp1902.fpage;
import android.media.Image;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.sp1902.R;
import com.example.sp1902.utils.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页碎片
 */
public class FirstPageFragment extends BaseFragment {
    ViewPager typeVp,lbVp;
    LinearLayout typeLayout,lpLayout;

    List<ImageView>lbviewList;
    List<ImageView>lbPointList;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                int currentItem = lbVp.getCurrentItem();
                if (currentItem == lbviewList.size()-1) {
                    lbVp.setCurrentItem(0);
                }else {
                    lbVp.setCurrentItem(currentItem+1);
                }
                handler.sendEmptyMessageDelayed(1,3000);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first_page, container, false);
        initView(view);
        setLBData();
        setLBListener();
        handler.sendEmptyMessageDelayed(1,3000);
        return view;
    }

    private void setLBListener() {
        lbVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                for (int i = 0;i < lbPointList.size();i++){
                    lbPointList.get(i).setImageResource(R.mipmap.page__normal_indicator);
                }
                lbPointList.get(position).setImageResource(R.mipmap.page__normal_indicator);
            }
        });
    }

    private void setLBData() {
        lbviewList = new ArrayList<>();
        lbPointList = new ArrayList<>();
        int[]resId = {R.mipmap.ad1,R.mipmap.ad2};

        for (int i = 0;i < resId.length;i++){
            ImageView iv = new ImageView(getContext());
            iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setImageResource(resId[i]);
            lbviewList.add(iv);

            ImageView piv = new ImageView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.setMargins(8,0,8,0);
            piv.setLayoutParams(params);
            piv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            piv.setImageResource(R.mipmap.page__normal_indicator);
            lpLayout.addView(piv);
            lbPointList.add(piv);
        }
        lbPointList.get(0).setImageResource(R.mipmap.page__normal_indicator);
        LunboAdapter lunboAdapter = new LunboAdapter(lbviewList);
        lbVp.setAdapter(lunboAdapter);
    }

    private void initView(View view) {
        typeVp=view.findViewById(R.id.frag_fp_vp_type);
        lbVp=view.findViewById(R.id.frag_fp_vp_lb);
        typeLayout=view.findViewById(R.id.frag_fp_layout_p1);
        lpLayout=view.findViewById(R.id.frag_fp_layout_p2);

    }

}
